/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import opiam.admin.faare.service.services.acl.AclPluginInterface;

import org.apache.log4j.Logger;

import java.io.Serializable;

import java.util.HashMap;
import java.util.Map;


/**
 *
 * This object conveys ACLs defined in acl_conf.xml.
 *
 */
public class JBAcls implements Serializable
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBAcls.class);

    /** ACLs table. */
    private Map aclsMap = new HashMap();

    /** ACLs Plugin class name. */
    private String classname;

    /** ACLs Plugin class. */
    private AclPluginInterface plugin;

    /**
     * Adds one ACL.
     *
     * @param jbAcl ACL to add
     */
    public void addAcl(JBAcl jbAcl)
    {
        String key = null;

        aclsMap.put(jbAcl.getTargetname().trim().toLowerCase(), jbAcl);
    }

    /**
     * Returns the ACLs table.
     * @return Map containing the ACLs.
     */
    public Map getAclsMap()
    {
        return aclsMap;
    }

    /**
     * Returns the ACLs plugin class name.
     * @return ACLs plugin class name.
     */
    public String getClassname()
    {
        return classname;
    }

    /**
     * Sets the ACLs plugin class name.
     * @param aclassname The ACLs plugin class name to set.
     */
    public void setClassname(String aclassname)
    {
        this.classname = aclassname;

        // instancie un objet the ce plugin
        try
        {
            Class cl = Class.forName(classname);
            plugin = (AclPluginInterface) cl.newInstance();
        }
        catch (Exception ex)
        {
            _logger.error("pb dans setClassname");
            throw new RuntimeException("pb dans setClassname");
        }
    }

    /**
     * Displays the ACLs.
     *
     * @return String formatted ACLs.
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        buf.append("classname = ");
        buf.append(classname);
        buf.append(System.getProperty("line.separator"));

        if (aclsMap != null)
        {
            buf.append("Map components of aclsMap = ");
            buf.append(System.getProperty("line.separator"));
            it = aclsMap.keySet().iterator();

            Object key;

            while (it.hasNext())
            {
                buf.append("...");
                key = it.next();
                buf.append(key);
                buf.append(" = ");
                buf.append(aclsMap.get(key));
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    // end of toString method

    /**
     * Returns the ACL plugin class.
     * @return class implemnting AclPluginInterface
     */
    public AclPluginInterface getPlugin()
    {
        return plugin;
    }
}
